package com.jingbit.ai.receiver;


import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.jingbit.ai.base.BaseApp;


/**
 * @author pansha
 */
@SuppressLint("LongLogTag")
public class NetworkReceiver extends BroadcastReceiver {
    private static final String TAG = BaseApp.TAG + ":NetworkReceiver";
    private static final int MIN_RSSI = -100;
    private static final int MAX_RSSI = -55;
    private static final int RSSI_LEVELS = 5;

    public static int networkType = -1;    //0表示wifi；1表示数据网络
    public static boolean isAvailable;

    private static WifiManager wifiManager;
    private static ConnectivityManager cManager;

    /**
     * 初始化网络设置
     */
    public static void initNetworkManager(Context context) {
        wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        cManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        setNetworkState(true);
    }

    private static boolean poweron;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(TAG, "onReceive: " + action + "---" + poweron);
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            setNetworkState(false);
            if (isAvailable && poweron) {
                poweron = false;
//				Intent intent1=new Intent(Constants.CHAT_SERVICE_ACTION);
//				intent1.putExtra("poweron", true);
//				context.startService(intent1);
            }
        } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            poweron = true;
        } else if (action.equals(Intent.ACTION_SHUTDOWN)) {
//			context.stopService(new Intent(Constants.CHAT_SERVICE_ACTION));
        }
    }

    private static void setNetworkState(boolean isInit) {
        NetworkInfo networkInfo = cManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            State state = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                    .getState(); // 获取网络连接状态
            Log.d(TAG, "setNetworkState wifi：" + state.name());
            if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络
                isAvailable = true;
                networkType = 0;
                if (!isInit) {
                    BaseApp.application.networkChanged(networkType, isAvailable);
                }
                return;
            }
            state = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                    .getState(); // 获取网络连接状态
            Log.d(TAG, "setNetworkState gprs：" + state.name());
            if (State.CONNECTED == state) { // 判断是否正在使用GPRS网络
                isAvailable = true;
                networkType = 1;
            }
        } else {
            isAvailable = false;
            networkType = -1;
        }
        if (!isInit) {
            BaseApp.application.networkChanged(networkType, isAvailable);
        }
    }


    public static void getWifiState(int wifi_state) {
        Log.d(TAG, "getWifiState：" + wifi_state);
        switch (wifi_state) {
            case WifiManager.WIFI_STATE_DISABLING:
                break;
            case WifiManager.WIFI_STATE_DISABLED:
                break;
            case WifiManager.WIFI_STATE_ENABLING:
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                break;
            case WifiManager.WIFI_STATE_UNKNOWN:
                break;
        }
    }

    public static String getWifiState() {
        int wifi_state = wifiManager.getWifiState();
        WifiInfo info = wifiManager.getConnectionInfo();
        Log.d("getWifiState", wifi_state + "/" + info);
        switch (wifi_state) {
            case WifiManager.WIFI_STATE_DISABLING:
                return "正在断开";
            case WifiManager.WIFI_STATE_DISABLED:
                return "不可用";
            case WifiManager.WIFI_STATE_ENABLING:
                return "正在连接";
            case WifiManager.WIFI_STATE_ENABLED:
                int singal = getSignalLevel(info.getRssi(), 5);
                if (singal != 0 && isAvailable) {
                    return "已连接" + singal + "格信号";
                }
                return "未连接";
            default:
                return "未知状态";
        }
    }

    public static int getSignalLevel(int rssi, int numLevels) {
        if (rssi <= MIN_RSSI) {
            return 0;
        } else if (rssi >= MAX_RSSI) {
            return numLevels - 1;
        } else {
            int partitionSize = (MAX_RSSI - MIN_RSSI) / (numLevels - 1);
            return (rssi - MIN_RSSI) / partitionSize;
        }
    }

    public static void setWifiState() {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        } else {
            wifiManager.setWifiEnabled(true);
        }
    }
}
